
//
// 120305 - AH - Created.
//

package org.soekrat.gui.swing.dictionary;

import java.awt.BorderLayout;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.soekrat.engine.dictionary.Dictionary;
import org.soekrat.engine.dictionary.SimpleDictionary;

/**
 * <p>
 *   Word list view for a 
 *   {@link Dictionary}.
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class DictionaryWordList extends JPanel
{
  private final DefaultListModel m_model=new DefaultListModel();
  
  private final JList m_list=new JList(m_model);
  
  private Dictionary m_dict;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public DictionaryWordList(){ this(null); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param dict {@link Dictionary} to view, if {@code null} creates a
   *             {@link SimpleDictionary}.
   */
  public DictionaryWordList(Dictionary dict)
  {
    m_dict=(dict==null) ? new SimpleDictionary() : dict;
    
    setLayout(new BorderLayout());
    
    add(new JScrollPane(m_list), BorderLayout.CENTER);
    
    refresh();
  }
  
  /**
   * <p>
   *   Sets
   *   {@link Dictionary} to view.
   * </p>
   * @param dict {@link Dictionary} or {@code null} if to clear.
   */
  public void setDictionary(Dictionary dict){ m_dict=dict; refresh(); }
  
  /**
   * <p>
   *   Refresh view.
   * </p>
   */
  public void refresh()
  {
    m_model.removeAllElements();
    if (m_dict!=null)
    {
      String[] words=m_dict.getWords();
      for (String word : words) m_model.addElement(word);
    }
  }
  
  /**
   * <p>
   *   Adds word to dictionary.
   * </p>
   * <p>
   *   There is no need to call
   *   {@link #refresh()} after this call.
   * </p>
   * @param word Word to add.
   * @return {@code true} if word was added and {@code false} if word already in
   *         dictionary.
   */
  public boolean addWord(String word)
  {
    if (word==null)
    {
      throw new NullPointerException("word");
    }
    
    if (m_dict==null) m_dict=new SimpleDictionary();
      
    if (m_dict.add(word)) 
    {
      refresh();
      return true;
    }
    return false;
  }
  
  /**
   * <p>
   *   Removes word to dictionary.
   * </p>
   * <p>
   *   There is no need to call
   *   {@link #refresh()} after this call.
   * </p>
   * @param word Word to remove.
   * @return {@code true} if word was added and {@code false} if word already in
   *         dictionary.
   */
  public boolean removeWord(String word)
  {
    if (word==null)
    {
      throw new NullPointerException("word");
    }
    
    if (m_dict==null) return false;
    
    if (m_dict.remove(word)) 
    {
      refresh();
      return true;
    }
    return false;
  }
  
  /**
   * <p>
   *   Gets
   *   {@link Dictionary} viewed.
   * </p>
   * @return {@link Dictionary}.
   */
  public Dictionary getDictionary(){ return m_dict; }
  
}
